package com.example.ykbboom.universitytransportation.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.ykbboom.universitytransportation.R;
import com.example.ykbboom.universitytransportation.Util.SharedPrefUtil;
import com.example.ykbboom.universitytransportation.Util.ToastUtil;

public class BecomeDriverActivity extends BaseActivity {

    private static final int START_PLACE_CODE = 1;
    private Toolbar mToolbar;
    private Button becomeDriverBtn;
    private TextView startPlaceText;
    private LinearLayout layout;
    private Intent intent;
    private String latlongStart;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_become_driver);
        initView();
    }

    @Override
    public void initView() {
        super.initView();
        mToolbar = (Toolbar) findViewById(R.id.toolbar_become_driver);
        setSupportActionBar(mToolbar);
        ActionBar actionBar = getSupportActionBar();
        if(actionBar!=null){
            actionBar.setHomeButtonEnabled(true); //设置返回键可用
            actionBar.setDisplayHomeAsUpEnabled(true);//显示HomeAsUp按钮
            actionBar.setDisplayShowTitleEnabled(false);//不自动显示标题
        }
        becomeDriverBtn = (Button) findViewById(R.id.become_driver_btn);
        becomeDriverBtn.setOnClickListener(this);
        startPlaceText = (TextView) findViewById(R.id.activity_driver_start_place);
        layout = (LinearLayout) findViewById(R.id.location_ll_driver_start_place);
        layout.setOnClickListener(this);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
        }
        return true;
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.location_ll_driver_start_place:
                intent = new Intent(this,ChoosePlaceActivity.class);
                startActivityForResult(intent, START_PLACE_CODE);
                break;
            case R.id.become_driver_btn:
                if (latlongStart==null){
                    ToastUtil.showToast(this,"请选择起点");
                    break;
                }else if (SharedPrefUtil.getDriverState()){
                    ToastUtil.showToast(this,"您已经是车主啦");
                    break;
                }else {
                    SharedPrefUtil.saveDriverState(true);//成为车主
                    SharedPrefUtil.saveDriverLatlong(latlongStart);//存储车主起点
                    ToastUtil.showToast(this,"可以在首页接单咯");
                    finish();
                }
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch(requestCode){
            case START_PLACE_CODE:
                if (resultCode==RESULT_OK){
                    startPlaceText.setText(data.getStringExtra("place"));
                    latlongStart = data.getStringExtra("latlong");
                }
                break;
        }
    }
}
